<template>
    <div>
        <!-- 表头 -->
        <div class="Head" v-if="Head">
            <!-- 仓库名称 -->
            <div class="Cusname">
                <span style="margin-right: 10px">仓库名称</span>
                <span>
                    <el-input v-model="adi.name" placeholder="请输入名称"></el-input>
                </span>
            </div>
            <!-- 货架名称 -->
            <div class="Cusname">
                <span style="margin-right: 10px">货架名称</span>
                <span>
                    <el-input v-model="adi.address" placeholder="请输入名称"></el-input>
                </span>
            </div>
            <!-- 搜索，重置按钮 -->
            <div class="But">
                <el-button type="primary" icon="el-icon-search" @click="serch">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="Reset()">重置</el-button>
            </div>
        </div>
        <!-- 表 -->
        <div class="function" style="margin-top: 10px">
            <el-button type="primary" icon="el-icon-plus" plain @click="addLists">新增</el-button>
            <el-button type="warning" icon="el-icon-download" plain>导出</el-button>
            <div style="float: right">
                <el-tooltip class="item" effect="dark" content="隐藏搜索" placement="top">
                    <el-button icon="el-icon-search" circle @click="Display"></el-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="刷新" placement="top">
                    <el-button icon="el-icon-refresh" circle @click="Reset"></el-button>
                </el-tooltip>
            </div>
        </div>
        <el-table
            :data="tableData"
            class="content"
            :cell-style="rowClass"
            :header-cell-style="headClass"
        >
                <el-table-column label="序号" type="index" :index="indexMethod"></el-table-column>
            <el-table-column prop="name" label="仓库名称"></el-table-column>
            <el-table-column prop="address" label="仓库地址"></el-table-column>
            <el-table-column prop="code" label="仓库条码"></el-table-column>
            <el-table-column prop="contacts" label="联系人"></el-table-column>
            <el-table-column prop="phone" label="手机号码"></el-table-column>
            <el-table-column prop="category" label="类别"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="update(scope.row)" class="el-icon-edit">修改</el-button>
                    <el-button type="text" size="small" @click="Deldeli(scope.row.wId)" class="el-icon-delete">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="block">
            <el-pagination
                background
                style="float: right"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page.pageNum"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="page.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
            >
            </el-pagination>
        </div>
        <!-- 添加仓库弹框 -->
        <el-dialog title="添加仓库" :visible.sync="dialogVisible" width="55%" :before-close="handleClose" class="adframe">
            <el-form ref="form" :model="adi">
                <span style="display: block; margin-top: -20px"
                    >仓库名称<el-input placeholder="请输入仓库名称" v-model="adi.name"></el-input
                ></span>
                <span>地址<el-input placeholder="请输入地址" v-model="adi.address"></el-input></span>
                <span>条码<el-input placeholder="请输入条码" v-model="adi.code"></el-input></span>
                <span>联系人<el-input placeholder="请输入联系人" v-model="adi.contacts"></el-input></span>
                <span>手机号码<el-input placeholder="请输入手机号码" v-model="adi.phone"></el-input></span>
                <span>类别<el-input placeholder="请输入类别" v-model="adi.category"></el-input></span>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dw">取 消</el-button>
                <el-button type="primary" @click="conVarSum">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { listWarehouse, dele, upt, add } from '../../api/user.js';
export default {
    data() {
        return {
            // 搜索输入框
            value: '',
            // 表头隐藏
            Head: true,
            // 添加框
            dialogVisible: false,
            tableData: [],
            // 分页
            page: {
                pageSize: 10,
                pageNum: 1
            },
            // 添加输入框
            adi: {
                // 地址
                address: null,
                // 类别
                category: null,
                // 条码
                code: null,
                // 联系人
                contacts: null,
                // 名称
                name: null,
                // 手机号码
                phone: null
            },
            total:null
        };
    },
    methods: {
        // 重置按钮
        Reset() {
            this.Rean();
            this.adi.name = undefined;
            this.adi.address = undefined;
        },
        // 表头显示
        Display() {
            this.Head = !this.Head;
        },
        // 表头样式设置
        headClass() {
            return 'text-align: center;background:#F8F8F9;color:#515A6D;';
        },
        // 表格样式设置
        rowClass() {
            return 'text-align: center;';
        },
        // 分页点击
        indexMethod(index) {
            return (this.page.pageNum - 1) * this.page.pageSize + index + 1;
        },
        // 分页
        handleSizeChange(val) {
            this.page.pageSize = Number(val);
            console.log(`每页 ${val} 条`);
            this.Rean()
        },
        handleCurrentChange(val) {
            this.page.pageNum = Number(val);
            console.log(`当前页: ${val}`);
            this.Rean()
        },
        // 表格渲染
        Rean() {
            listWarehouse(this.page).then((a) => {
                this.tableData = a.rows;
                this.total=a.total
            });
        },
        // 搜索
        serch() {
            listWarehouse(this.adi).then((res) => {
                console.log(res);
                this.tableData=res.rows
            });
        },
        // 删除
        Deldeli(index) {
            console.log(index);
            this.wId = index;
            dele(this.wId).then((res) => {
                console.log(res);
                this.Rean();
            });
        },
        //
        resect() {
            // 地址
            (this.adi.address = null),
                // 类别
                (this.adi.category = null),
                // 条码
                (this.adi.code = null),
                // 联系人
                (this.adi.contacts = null),
                // 名称
                (this.adi.name = null),
                // 手机号码
                (this.adi.phone = null);
        },
        //   添加弹框
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then((_) => {
                    done();
                })
                .catch((_) => {});
        },
        dw() {
            this.dialogVisible = false;
        },
        //新增按钮
        addLists() {
            this.resect();
            this.dialogVisible = true;
            this.Dialogtitle = '添加仓库';
            this.conVar = 0;
        },
        //修改按钮
        update(k) {
            this.Dialogtitle = '修改仓库';
            this.conVar = 1;
            this.adi = k;
            this.dialogVisible = true;
        },
        //弹框提交
        conVarSum() {
            this.dialogVisible = false;
            if (this.conVar == 0) {
                add(this.adi).then((res) => {
                    console.log(res);
                    this.Rean();
                    this.$message.success('添加成功');
                });
            } else {
                upt(this.adi).then((res) => {
                    console.log(res);
                    this.Rean();
                    this.$message.success('修改成功');
                });
            }
        }
    },
    mounted() {
        this.Rean();
    }
};
</script>

<style scoped>
.Head {
    height: 50px;
}
/* 客户类别 */
.Cusgory {
    height: 100%;
    float: left;
    margin-right: 10px;
}
.Cusgory span {
    display: block;
    height: 100%;
    line-height: 50px;
    font-weight: 800;
    color: #606266;
    float: left;
}
/* 客户名称 */
.Cusname {
    height: 100%;
    float: left;
    margin-right: 10px;
}
.Cusname span {
    display: block;
    height: 100%;
    line-height: 50px;
    font-weight: 800;
    color: #606266;
    float: left;
}
/* 搜素，重置按钮 */
.But {
    height: 100%;
    float: left;
}
.But button {
    margin-top: 10px;
}
</style>